﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Xiami.Helper.Model;
using AMing.Extension.Net40;
using Html5Music.Data;
using AMing.Helper.Net40;
using System.Net;
using MusicForMvc3.Method.Cache;

namespace MusicForMvc3.Controllers
{
    public class HomeController : Controller
    {
        #region Data
        private PlayListData _playlist = null;

        public PlayListData Playlist
        {
            get
            {
                if (_playlist == null)
                {
                    Html5Music.Data.Config.Path_PlayList = Server.MapPath("~/file/playlist.txt");
                    _playlist = new Html5Music.Data.PlayListData();
                }
                return _playlist;
            }
        }
        private UserListData _userlist = null;

        public UserListData Userlist
        {
            get
            {
                if (_userlist == null)
                {
                    Html5Music.Data.Config.Path_UserList = Server.MapPath("~/file/userlist.txt");
                    _userlist = new Html5Music.Data.UserListData();
                }
                return _userlist;
            }
        }
        #endregion
        //
        // GET: /Home/

        public ActionResult Index()
        {
            #region temp

            //Userlist.Update(new Html5Music.Model.User
            //{
            //    //ID = Guid.NewGuid(),
            //    ID = Guid.Parse("b134950f-15b7-40cf-a7af-5a16c9c28d1e"),
            //    Access = Method.UserAccess.User.ToString(),
            //    LoginName = "aming",
            //    Password = "pwd2443",
            //    UserName = "A;Ming"
            //});
            //var user = Userlist.Login("y2443", "pwd2443");

            //Userlist.Delete(Guid.Parse("8196df5a-0187-4442-b3fc-f015d96e3e61"));


            //Playlist.Add(new Html5Music.Model.PlayList
            //{
            //    UserID = user.ID,
            //    Share = true,
            //    XiamiPageUrl = "http://www.xiami.com/album/1386742453",
            //    XiamiPlayUrl = "http://www.xiami.com/song/play?ids=/song/playlist/id/1386742453/type/1",
            //    Key = "aria",
            //    Title = "ARIA  by yu-yu"
            //});
            #endregion

            Method.Data.GetUserList GetUserList = new Method.Data.GetUserList(Userlist);
            ViewBag.List = GetUserList.Cache_GetObject(this.HttpContext);

            return View();
        }

        public ActionResult Player(string url, string key, Guid? guid)
        {
            string playurl = url;
            Html5Music.Model.PlayList playlist = null;
            if (!string.IsNullOrWhiteSpace(key))
            {
                Method.Data.GetPlaylistByKey GetPlaylistByKey = new Method.Data.GetPlaylistByKey(Playlist) { Key = key };
                playlist = GetPlaylistByKey.Cache_GetObject(this.HttpContext);
            }
            if (guid.HasValue)
            {
                Method.Data.GetPlaylistByID GetPlaylistByID = new Method.Data.GetPlaylistByID(Playlist) { ID = guid.Value };
                playlist = GetPlaylistByID.Cache_GetObject(this.HttpContext);
            }
            if (playlist != null && playlist.Share)
            {
                playurl = playlist.XiamiPlayUrl;
                ViewBag.Des = "<br/><a href='{0}' target='_blank'>{1}</a>".ToFromat(playlist.XiamiPageUrl, playlist.Title);
            }
            ViewBag.PlayListUrl = playurl;

            return View();
        }

        public ActionResult PlayList(string url)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(url))
                {
                    return Json("url is null.", JsonRequestBehavior.AllowGet);
                }

                Method.Data.GetXiami GetXiami = new Method.Data.GetXiami { Url = url };
                var music_list = GetXiami.Cache_GetObject(this.HttpContext);

                return Json(music_list, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                string msg = string.Format("Error:{0}\n{1}\n{2}",
                      ex.Message,
                      ex.Source,
                      ex.StackTrace);
                return Json(msg, JsonRequestBehavior.AllowGet);
            }
        }


        public ActionResult PlaylistForUser(string key)
        {
            if (string.IsNullOrWhiteSpace(key))
            {
                return HttpNotFound();
            }
            Method.Data.GetByLoginName GetByLoginName = new Method.Data.GetByLoginName(Userlist) { Key = key };
            var user = GetByLoginName.Cache_GetObject(this.HttpContext);
            if (user == null)
            {
                return HttpNotFound();
            }

            Method.Data.GetPlaylistByUserID GetPlaylistByUserID = new Method.Data.GetPlaylistByUserID(Playlist) { UserID = user.ID };
            var list = GetPlaylistByUserID.Cache_GetObject(this.HttpContext);

            ViewBag.UserName = user.UserName;
            ViewBag.List = list;

            return View();
        }

        public ActionResult ClearCache()
        {
            Method.Cache.CacheHelper.ClearAllCache(this.HttpContext);

            return Json("clear cache complete.", JsonRequestBehavior.AllowGet);
        }


    }
}














//static public object ChangeType(object value, Type type)
//{
//    if (value == null && type.IsGenericType) return Activator.CreateInstance(type);
//    if (value == null) return null;
//    if (type == value.GetType()) return value;
//    if (type.IsEnum)
//    {
//        if (value is string)
//            return Enum.Parse(type, value as string);
//        else
//            return Enum.ToObject(type, value);
//    }
//    if (!type.IsInterface && type.IsGenericType)
//    {
//        Type innerType = type.GetGenericArguments()[0];
//        object innerValue = ChangeType(value, innerType);
//        return Activator.CreateInstance(type, new object[] { innerValue });
//    }
//    if (value is string && type == typeof(Guid)) return new Guid(value as string);
//    if (value is string && type == typeof(Version)) return new Version(value as string);
//    if (!(value is IConvertible)) return value;
//    return Convert.ChangeType(value, type);
//}